package com.pointi.simGatewayM;

import java.util.concurrent.ConcurrentLinkedQueue;

import com.pointi.emoTools.emoFileInfo;
import com.pointi.emoTools.emoLogManager;

public class emoSimulator
{
	public static void main(String[] args) throws Exception 
	{
		//____________________________________________________________________________________________________
		// CONFIGURATION

		String str_conffile = "simGatewayM.conf" ;
		ConfigurationData conf_data = new ConfigurationData() ;
		conf_data.InitializeData() ;
		try {
			conf_data = conf_data.ReadConfXML(str_conffile) ;
			conf_data.WriteLogConfigurationData(conf_data) ;
		} catch (Exception e) {
			System.out.println("[SM] Can't Read XML File!! - "+str_conffile) ;
			e.printStackTrace() ;
		}

		//____________________________________________________________________________________________________
		// GET FILE (File, FileList, FilePath)

		emoFileInfo mFileInfo = emoFileInfo.fnGetFileArray(conf_data.s_pcmfile_path) ;

		//____________________________________________________________________________________________________
		// RUN SENDER
		
		ConcurrentLinkedQueue<ThreadSender> activeThreads = new ConcurrentLinkedQueue<ThreadSender>() ;
		int tCNT = 0 ;
		int mIndex = 0 ;
		int mThreadLimit = Integer.valueOf(conf_data.s_thread_limit) ;
		int mThreadCount = 0 ;
		boolean loop = true ;

		while(loop)
		{
			if (activeThreads.isEmpty()) { activeThreads = new ConcurrentLinkedQueue<ThreadSender>() ; }
			mThreadCount = activeThreads.size() ;
			if (mThreadCount < mThreadLimit)
			{
				new ThreadSender(activeThreads).start(conf_data, mFileInfo.getFileList(mIndex), mFileInfo.getFile(mIndex), mFileInfo._FileList[mIndex], mFileInfo._File[mIndex]) ;
				mIndex++ ;
				tCNT ++ ;
			}
			if (mIndex == mFileInfo._File.length) { mIndex = 0 ; }
			if (tCNT == Integer.valueOf(conf_data.s_file_limit)) { loop = false ; }
		}
		emoLogManager.mLog("[SM] END") ;
	}
	
	//____________________________________________________________________________________________________
	// EOC
}
